package com.orangelife.mobile.main.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.Toast;

import com.common.sp.JsonSp;
import com.curry.android.http.OrangeErrorHandler;
import com.curry.android.http.VolleyHttpUtil;
import com.curry.android.net.NetworkUtil;
import com.curry.android.util.JSONHelper;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.main.adapter.FeatruesAdapter;
import com.orangelife.mobile.main.biz.MainHttpBz;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 服务
 */
public class Fragment1 extends Fragment implements OnClickListener {

    private View view;
    private GridView gvFeatures;

    private List<Map<String, Object>> data_list;

    private FeatruesAdapter featruesAdapter;

    public Fragment1() {
        super();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_server1,
                    container, false);
            initView();
            findView();
            requestFeatures();
        }
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null) {
            parent.removeView(view);
        }
        return view;
    }

    private void initView() {
        gvFeatures = (GridView) view.findViewById(R.id.gv_features);
        data_list = new ArrayList<>();
        featruesAdapter = new FeatruesAdapter(getActivity(), data_list);
        gvFeatures.setAdapter(featruesAdapter);
    }

    private void findView() {
        String json = JsonSp.getHouseKeeperMenu1Json();
        if (!TextUtils.isEmpty(json)) {
            featruesAdapter.setList((List<Map<String, Object>>) JSONHelper.jsonToMap(json).get("pageEntity"));
            featruesAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onClick(View view) {
        if (!NetworkUtil.isConnectedNet(getActivity())) {
            Toast.makeText(getActivity(),
                    getResources().getString(R.string.net_disconnected),
                    Toast.LENGTH_SHORT).show();
            return;
        }
    }


    /**
     * 获得分类列表
     */
    public void requestFeatures() {
        if (data_list != null && data_list.size() > 0) {
            return;
        }
        MainHttpBz.requestFeatures(new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int errCode) {
                data_list = MainHttpBz.handleFeatures(response);
                if (data_list != null && data_list.size() > 0) {
                    featruesAdapter.setList(data_list);
                    featruesAdapter.notifyDataSetChanged();
                    JsonSp.setHouseKeeperMenu1Json(response.toString());
                }
            }
        }, MainHttpBz.FETRUES_TYPE_1);
    }

}
